package com.moniteach.server.servlet;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;

import com.moniteach.server.db.bo.Group;
import com.moniteach.server.db.bo.UserInfo;
import com.moniteach.server.db.connector.PMF;
import com.moniteach.utils.Resource;

public class GroupServlet extends CommonServlet {
	private String action;
	private String id;
	private String name;
	private String description;
	private String starosta;

	@Override
	protected void prepare(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		resp.setCharacterEncoding("UTF-8");

		action = req.getParameter(Resource.Const.action);
		if ("".equals(action) || action == null) {
			resp.getWriter().print(Resource.Const.no + "ACTION");
			return;
		}

		id = req.getParameter(Resource.Const.id);
		name = req.getParameter(Resource.Const.name);
		description = req.getParameter(Resource.Const.description);
		starosta = req.getParameter(Resource.Const.starosta);

		String response = "";
		if (Resource.Action.CREATE.equals(action)) {
			response = create();
		} else if (Resource.Action.GET_BY_ID.equals(action)) {
			response = getById();
		} else if (Resource.Action.MODIFY.equals(action)) {
			response = modify();
		} else if (Resource.Action.GET_ALL.equals(action)) {
			response = getAll();
		}
		resp.getWriter().print(response);
	}

	@Override
	protected String create() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Long starostaLong = Long.valueOf(starosta);
			UserInfo entityUser = pm
					.getObjectById(UserInfo.class, starostaLong);
			Group oneGroup = new Group(name, description, entityUser.getId());
			pm.makePersistent(oneGroup);
			return String.valueOf(oneGroup.getId());
		} catch (Exception e) {
			return Resource.Const.no;
		} finally {
			pm.close();
		}
	}

	@Override
	protected String get() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String getById() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Long idLong = Long.valueOf(id);
			Group entityGroup = pm.getObjectById(Group.class, idLong);
			return entityGroup.toJSONObject().toString();
		} catch (Exception e) {
			return Resource.Const.no;
		} finally {
			pm.close();
		}
	}

	@Override
	protected String getAll() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Group.class);
		try {
//			query.setFilter("visibility == true");
			List<Group> results = (List<Group>) query.execute();
			JSONArray array = new JSONArray();
			if (results.size() > 0) {
				for (Group group: results) {
					array.put(group.toJSONObject());
				}
				return array.toString();
			} else
				return Resource.Const.no;

		} catch (Exception e) {
			return Resource.Const.no + e;
		} finally {
			pm.close();
		}
	}

	@Override
	protected String modify() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Long idLong = Long.valueOf(id);
			Group entityGroup = pm.getObjectById(Group.class, idLong);

			if (name != null) {
				entityGroup.setName(name);
			}
			if (description != null) {
				entityGroup.setDescription(description);
			}
			if (starosta != null) {
				Long starostaLong = Long.valueOf(starosta);
				UserInfo entityUser = pm.getObjectById(UserInfo.class,
						starostaLong);
				entityGroup.setStarosta(entityUser.getId());
			}
			pm.makePersistent(entityGroup);
			return Resource.Const.ok;
		} catch (Exception e) {
			return Resource.Const.no;
		} finally {
			pm.close();
		}
	}

}
